package com.stu.dao.impl;

import com.stu.dao.TeacherDao;
import com.stu.model.StudentAnswer;
import com.stu.model.Task;
import com.stu.model.User;
import com.stu.util.JdbcUtils;
import com.stu.util.DaoUtile;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.stu.util.DaoUtile.isExist;

public class TeacherDaoImpl implements TeacherDao {
    //根据老师的账号查看属于自己班级的学生
    @Override
    public List<User> SelectAllStudentByTeacherAccount(String teacherAccount) throws SQLException {
        String sql = "select * from user where userAccount in(select studentAccount from userteacher where teacherAccount='" + teacherAccount + "')";
        /* System.out.println(sql);*/
        return DaoUtile.getAllUser(sql);
    }


    //根据老师的账号查询老师发布的任务
    @Override
    public List<Task> getAllMyTask(String teacherAccount) throws SQLException {
        String sql = "select * from task where teacherAccount='" + teacherAccount + "'";
        /*     System.out.println(sql);*/
        return DaoUtile.getTask(sql);
    }


    //根据任务账户查询任务的详细信息即学生作答任务的情况
    @Override
    public List<StudentAnswer> getTaskDetail(String taskAccount) throws SQLException {
        String sql = "select s.*,u.userName,taskName from studenttask s inner join user u on s.studentAccount=u.userAccount " +
                "inner join task t on s.taskAccount=t.taskAccount where s.taskAccount='" + taskAccount + "'";
        System.out.println(sql);
        return DaoUtile.getTaskDetail(sql);
    }

    @Override
//通过随机生成的任务号,组成完整的查询语句
    public int isExistTaskAccount(String taskAccount) throws SQLException {
        String sql = "select count(*) num from task where taskAccount='" + taskAccount + "'";
        //将查询语句作为参数传递给执行查询语句的方法
        return isExist(sql);
    }


    //老师添加任务
    @Override
    public void addTask(Task task) throws SQLException {
        String taskAccount = task.getTaskAccount();
        String teacherAccount = task.getTeacherAccount();
        String taskDate = task.getTaskDate();
        String taskName = task.getTaskName();

        String sql1 = "INSERT INTO task  VALUES ('" + taskAccount + "','" + teacherAccount + "','" + taskDate + "','" + taskName + "')";
        DaoUtile.notQuery(sql1);

        String sql3 = "INSERT INTO studenttask (taskAccount, studentAccount, studentAnswer, isFinish, score) " +
                "SELECT '" + taskAccount + "', B.C, null, 0, 0 FROM (SELECT userteacher.studentAccount as C FROM userteacher WHERE teacherAccount = '" + teacherAccount + "') AS B";
        DaoUtile.notQuery(sql3);
    }

    //根据老师账户模糊查询属于老师的所有学生
    @Override
    public List<User> searchStudentByTeacherAccount(String userAccount, String searchText) throws SQLException {
        String sql = "select * from user where userAccount in (select userteacher.studentAccount from userteacher where  teacherAccount='" + userAccount + "') and (userAccount='" + searchText + "' or userName like '%" + searchText + "%' or userIdCard='" + searchText + "')";
        return DaoUtile.getAllUser(sql);
    }

    //根据老师账号和学生账号,查询隶属这个老师的学生的所有任务的作答详情
    @Override
    public List<StudentAnswer> goStudentTaskDetail(String teacherAccount, String studentAccount) throws SQLException {
        String sql = "SELECT s.*, u.userName , taskName FROM studenttask s INNER JOIN user u ON s.studentAccount = u.userAccount INNER JOIN task t ON s.taskAccount = t.taskAccount " +
                "WHERE s.studentAccount = '" + studentAccount + "' AND t.teacherAccount = '" + teacherAccount + "'";
        System.out.println(sql);

        return DaoUtile.getTaskDetail(sql);
    }
   //删除未完成任务的学生账号
    @Override
    public void deleteTask(String taskAccount, String studentAccount, String isFinish) throws SQLException {
        //0代表学生未完成
        if (Integer.parseInt(isFinish) == 0) {
            //直接删除
            String sql="delete from studentTask where taskAccount='"+taskAccount+"'and studentAccount='"+studentAccount+"'";
DaoUtile.notQuery(sql);
        }
    }
  //删除老师发布任务
    @Override
    public void deleteThisTask(String taskAccount) throws SQLException {
        String sql1="delete from task where taskAccount='"+taskAccount+"'";
        String sql2="delete from studenttask where taskAccount='"+taskAccount+"'";
        DaoUtile.notQuery(sql1);
        DaoUtile.notQuery(sql2);

    }
      //老师移除自己的这个学生
    @Override
    public void deleteThisPersonByTeacher(String studentAccount, String userAccount) throws SQLException {
        String sql1="delete from userteacher where studentAccount ='"+studentAccount+"'and teacherAccount='"+userAccount+"'";
    String sql2="delete from studenttask where taskAccount in(select taskAccount from task where teacherAccount='"+userAccount+"')and studentAccount='"+studentAccount+"'";
  DaoUtile.notQuery(sql1);
  DaoUtile.notQuery(sql2);
    }


}



